<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <h2>Essential Links</h2>
    <button @click="wechatLogin">微信扫描登录</button> /
    <button @click="wechatBind">微信扫描绑定</button> /
    <button @click="dingtalkLogin">钉钉扫描登录</button> /
    <button @click="dingtalkBind">钉钉扫描绑定</button>
  </div>
</template>

<script>
import Cookies from 'js-cookie'
import openWindow from '../utils/openWindow'
import fetch from '../utils/fetch'

export default {
  name: 'hello',
  data () {
    return {
      msg: 'Welcome to Your Vue.js App'
    }
  },
  methods: {
    wechatLogin: function () {
      console.log('wechat login')
      const self = this
      const hash = Math.random().toString(36).slice(5)
      fetch.Axios.post('/third/login/', {
        domain: 'guming',
        ftype: 'wechat_qrcode',
        hash: hash,
        href: window.location.href,
        action: 'signin'
      }).then(function (response) {
        console.log(response)
        let data = response.data
        if (!data.errcode) {
          openWindow(data.url, 'wechat', 540, 540)

          let interval = setInterval(function () {
            fetch.Axios.post('/third/check/', {
              hash: hash
            }).then(function (response) {
              console.log(response)
              let data = response.data
              if (!data.errcode) {
                self.msg = data.token
                Cookies.set('ptoken', data.token)
                // 另外设置过期时间，当前时间加上50分钟
                clearInterval(interval)
              } else {
                self.msg = data.errcode
              }
            })
          }, 3000)
        }
      })
    },
    wechatBind: function () {
      console.log('wechat bind')
      const self = this
      const hash = Math.random().toString(36).slice(5)
      fetch.Axios.post('/third/login/', {
        domain: 'guming',
        ftype: 'wechat_qrcode',
        hash: hash,
        href: window.location.href,
        action: 'bind'
      }).then(function (response) {
        console.log(response)
        let data = response.data
        if (!data.errcode) {
          openWindow(data.url, 'wechat', 540, 540)

          let interval = setInterval(function () {
            fetch.Axios.post('/third/check/', {
              hash: hash
            }).then(function (response) {
              console.log(response)
              let data = response.data
              if (!data.errcode) {
                self.msg = data.token
                Cookies.set('ptoken', data.token)
                // 另外设置过期时间，当前时间加上50分钟, 并且刷新页面
                clearInterval(interval)
              } else {
                self.msg = data.errcode
              }
            })
          }, 3000)
        }
      })
    },

    dingtalkLogin: function () {
      console.log('dingtalk login')
      const self = this
      const hash = Math.random().toString(36).slice(5)
      fetch.Axios.post('/third/login/', {
        domain: 'guming',
        ftype: 'dingtalk_qrcode',
        hash: hash,
        href: window.location.href,
        action: 'signin'
      }).then(function (response) {
        console.log(response)
        let data = response.data
        if (!data.errcode) {
          openWindow(data.url, 'dingtalk', 540, 540)

          let interval = setInterval(function () {
            fetch.Axios.post('/third/check/', {
              hash: hash
            }).then(function (response) {
              console.log(response)
              let data = response.data
              if (!data.errcode) {
                self.msg = data.token
                Cookies.set('ptoken', data.token)
                // 另外设置过期时间，当前时间加上50分钟
                clearInterval(interval)
              } else {
                self.msg = data.errcode
              }
            })
          }, 3000)
        }
      })
    },
    dingtalkBind: function () {
      console.log('dingtalk bind')
      const self = this
      const hash = Math.random().toString(36).slice(5)
      fetch.Axios.post('/third/login/', {
        domain: 'guming',
        ftype: 'dingtalk_qrcode',
        hash: hash,
        href: window.location.href,
        action: 'bind'
      }).then(function (response) {
        console.log(response)
        let data = response.data
        if (!data.errcode) {
          openWindow(data.url, 'dingtalk', 540, 540)

          let interval = setInterval(function () {
            fetch.Axios.post('/third/check/', {
              hash: hash
            }).then(function (response) {
              console.log(response)
              let data = response.data
              if (!data.errcode) {
                self.msg = data.token
                Cookies.set('ptoken', data.token)
                // 另外设置过期时间，当前时间加上50分钟, 并且刷新页面
                clearInterval(interval)
              } else {
                self.msg = data.errcode
              }
            })
          }, 3000)
        }
      })
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1,
h2 {
  font-weight: normal;
}

ul {
  list-style-type: none;
  padding: 0;
}

li {
  display: inline-block;
  margin: 0 10px;
}

a {
  color: #42b983;
}
</style>
